package com.ccxe.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 时间格式工具类
 * @author Administrator
 *
 */
public class DateConvertUtil {

	private static final String defaultTemplet = "yyyy-MM-dd HH:mm:ss";
	public static DateFormat getSimpleDF(String templet) {
		if(templet != null )
			return new SimpleDateFormat(templet);
		else
			return new SimpleDateFormat(defaultTemplet);
	}

	public static String getDate() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System
				.currentTimeMillis()));
	}
	
	public static Date stringToDate(String templet,String source) throws ParseException {
		return getSimpleDF(templet).parse(source);
	}
	
	public static String dateToString(String templet, long c_date_time) {
		return getSimpleDF(templet).format(new Date(c_date_time));
	}
	
	public static String dateToString(String templet, Date date) {
		return getSimpleDF(templet).format(date);
	}
	/**
	 * 将指定格式的字符串转换成yyyyMMddHHmmss格式；
	 * @param templet 输入字符串的格式，默认为yyyy-MM-dd HH:mm:ss格式；
	 * @param source 输入的字符串
	 * @return 0:转换成long型数据时出错，
	 * 			1：解析指定字符串时出错。
	 */
	public static long stringToYMDHMS(String templet, String source) {
		try {
			return Long.parseLong(dateToString("yyyyMMddHHmmss",stringToDate(null,source)));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return 0;
		} catch (ParseException e) {
			e.printStackTrace();
			return 1;
		}
		
	}
	
	public static long getHMS(long time) throws ParseException {
		 return stringToDate("HH:mm", getSimpleDF("HH:mm").format(new Date(time))).getTime();
		
	}
	
	public static long getHMS(String time) {
		try {
			return stringToDate("HH:mm",time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public static void main(String[] args) throws Exception {
//		String str = "9:0";
//		System.out.println(getHMS(str));
//		System.out.println(dateToString(null,new Date(5660000)));
		
		System.out.println(stringToYMDHMS(null,"2010-10-20 12:32:45"));
		
	}
	
}
